<template>
  <div class="box">
    <el-table :data="tableData" stripe>
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="password" label="密码"> </el-table-column>
      <el-table-column prop="email" label="邮箱" width="260"> </el-table-column>
      <el-table-column prop="nickname" label="昵称"> </el-table-column>
      <el-table-column label="操作" class="cz-btn">
        <template slot-scope="scope">
          <button @click="adminxg(scope.row)">修改</button>/<button
            @click="adminsc(scope.row)"
          >
            删除
          </button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 修改用户 -->
    <el-dialog title="修改用户" :visible.sync="adminxgb">
      <el-form :model="forms">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="forms.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="forms.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="forms.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="forms.nickname"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="adminxgqx = false">取 消</el-button>
        <el-button type="primary" @click="xgadd()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 新增用户 -->
    <el-button type="text" @click="dialogFormVisible = true">
      新增用户
    </el-button>

    <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="用户名">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickname"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

  <script>
import axios from "axios";
export default {
  data() {
    return {
      dialogTableVisible: false,
      dialogFormVisible: false,
      adminxgb: false,
      adminxgqx: false,
      form: {
        name: "",
        password: "",
        email: "",
        nickname: "",
      },
      forms: {
        username: "",
        password: "",
        email: "",
        nickname: "",
      },
      formLabelWidth: "120px",
      tableData: [],
    };
  },
  methods: {
    // async 异步函数
    async getUsers() {
      // 使用try catch捕获错误
      try {
        // 使用axios发送ajax请求，获取用户请求
        // 把请求的结果拿到给一个变量，await是搭配异步函数等待执行完才会执行下面的代码
        let res = await this.$http.get("admin/getUsers");
        // 打印返回的结果
        console.log(res);
        // 对象解构，拿到数据，相当于code = res.data.code msg = res.data.msg data = res.data.data;
        let { code, msg, data } = res.data;
        // 如果返回的码不是200就代表出错了，调用ElementUI显示出错内容，return阻止代码向下执行
        if (code !== 200) return this.$message.error(msg);
        // 否则代表成功，打印看下成功数据
        console.log(data);
        // 替换表格数据
        this.tableData = data;
      } catch (err) {
        // 如果发送就出错了就会在这里处理错误事件
        // 调用ElementUI 给一个错误提示
        this.$message.error(err.message);
      }
    },
    add() {
      // console.log(this.form);
      axios.post("http://localhost:8081/admin/add", this.form).then((res) => {
        this.dialogFormVisible = false;
        this.getUsers();
      });
    },

    xgadd() {
      // console.log(this.forms);
      axios
        .post("http://localhost:8081/admin/xgadd", this.forms)
        .then((res) => {
          // console.log(res);
          this.adminxgb = false;
          this.getUsers();
        });
    },

    adminxg(row) {
      this.adminxgb = true;
      this.forms = row;
      this.forms.id = row.id;
      console.log(this.forms);
    },
    adminsc(row) {
      this.forms.id = row.id;
      console.log(this.forms.id);
      axios
        .post("http://localhost:8081/admin/adminsc", this.forms)
        .then((res) => {
          this.getUsers();
        });
    },
  },
  // 页面被挂载时候触发
  mounted() {
    this.getUsers();
  },
};
</script>
<style >
.box {
  width: 100%;
}
</style>